<?php

/**
 * Update resource image url, doesnot need to invoke douban API every time
 * using jsonp
 *
 * @author Jensen
 */
class search_typeahead extends AbstractAction {

    public function run() {
        $keyword = cleanInput($_GET['q']);

        $searchFile = "data/final_output";
        if(file_exists($searchFile)){
            $file = @fopen($searchFile, "r") ;
            $result = array();
            while (!feof($file))
            {
                // get 10 mactches
                if( count($result) >=10 ){
                    break;
                }
                
                $currentLine = fgets($file);
                if( startswith($currentLine, $keyword) ){
                    $result[] = $currentLine;
                }
            }   
            fclose($file) ;
            
            $this->json_output( 0, $result );
        }
        else{
            $this->json_output( 1, array() );
        }
    }


    /**
    * output json result
    * 
    * @param type $iRetcode
    * @param type $sErrorMsg
    * @param type $vmResult 
    */
    private function json_output( $iRetcode, $arrResult = array()) {
        $res = array(
            'result' => $iRetcode,
            'data' => $arrResult
        );

        echo json_encode( $res );
    }
}

?>
